package com.jl.js.core;

import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSStaticFunction;

import com.jl.util.HexUtil;

public class JSAssert extends ScriptableObject {

	private static final long serialVersionUID = -6602656459205756169L;

	@JSStaticFunction
	public static void isTrue(boolean b) {
		if (!b) {
			fail("failed");
		}
	}

	@JSStaticFunction
	public static void isFlase(boolean b) {
		if (!b) {
			fail("failed");
		}
	}

	@JSStaticFunction
	public static void isNull(Object obj) {
		if (obj != null) {
			fail("failed");
		}
	}

	@JSStaticFunction
	public static void isNotNull(Object obj) {
		if (obj == null) {
			fail("failed");
		}
	}

	@JSStaticFunction
	public static void equals(Object obj1, Object obj2) {
		if (!obj1.equals(obj2)) {
			fail("obj1 is " + obj1 + ",obj2 is " + obj2);
		}
	}

	@JSStaticFunction
	public static void hexEquals(String hex1, String hex2) {
		byte[] array = HexUtil.parseHexToByteArray(hex1);
		byte[] array2 = HexUtil.parseHexToByteArray(hex2);
		if (!HexUtil.arrayEqusls(array, array2)) {
			fail("Hex1 is " + hex1 + ",Hex2 is " + hex2);
		}
	}
	
	@JSStaticFunction
	public static void compareHex(String exp, String real) {
		if (!HexUtil.compare(exp, real)) {
			fail("Exp is " + exp + ",Real is " + real);
		}
	}

	@JSStaticFunction
	public static void fail(String message) {
		throw new RuntimeException(message);
	}

	@Override
	public String getClassName() {
		return "Assert";
	}
}